package com.gg.fruit.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.category.service.CategoryService;
import com.gg.entity.Category;
import com.gg.entity.Fruit;
import com.gg.fruit.service.FruitService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import base.BaseServlet;


@WebServlet("/fruit/GetHotFruitsServlet")
public class GetHotFruitsServlet extends BaseServlet {
	@Autowired
	private FruitService  fruitService;
	
	@Autowired
	private  CategoryService   categoryService;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//先要 获取 全部水果的 类别
		List<Category>  categorys = categoryService.getAllCategorys();
		request.getSession().setAttribute("categorys", categorys);
		
		// 
		String  flag =  request.getParameter("flag");
		
		
		if(flag!=null && flag.equals("nopage")) {
			Fruit  fruit =  new Fruit();
			fruit.setFhot(true);;
			List<Fruit>  fruits = fruitService.getFruitsBySearch(fruit);
			request.getSession().setAttribute("fruits", fruits);
			response.sendRedirect("/fruits/sel.jsp");
			return  ; 
			
		}

		//获取想要第几页  这个请求参数
		Integer pageNum =Integer.parseInt(request.getParameter("pageNum"))  ;
		// 分页      （执行拦截）
		PageHelper.startPage(pageNum,3);
		//设置 查询条件   
		Fruit  fruit  =   new Fruit();
		fruit.setFhot(true);
		//   业务层查询数据库后 返回结果集
		List<Fruit>  fruits =  fruitService.getFruitsBySearch(fruit);
		for (Fruit f :
				fruits) {
			System.out.println(f);
		}
		//  把分页信息  封装成    info 对象
		PageInfo<Fruit>   info =  new  PageInfo<Fruit>(fruits);
		
		//把分页信息  放入到session中
		request.getSession().setAttribute("hotinfo", info);
		// 重新回到  index.jsp这个界面
		response.sendRedirect("/fruits/index.jsp");
	}

	

}
